package com.dk;

import com.dk.common.Constants;
import com.dk.common.GlobalVariable;
import com.dk.frame.MainFrame;
import com.dk.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.awt.*;
import java.io.File;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 启动类
 *
 * @author: A11181121050450
 * @date: 2020-02-28 16:07
 */
@Slf4j
@SpringBootApplication
public class TransferFileApplication {
    public static void main(String[] args) {
        try {
            System.setProperty("java.awt.headless", "false");
//            System.setProperty("user.dir", PathUtil.getRootDir());
            Object[] closeArgs = new Object[2];
            EventQueue.invokeLater(() -> {
                try {
                    new MainFrame(() -> {
                        try {
                            OutputStream outputStream = Files.newOutputStream(Paths.get(((FileSystemResource)closeArgs[1]).getFile().getAbsolutePath()));
                            Constants.PROPERTIES.store(outputStream, "");
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                        } finally {
                            SpringApplication.exit((ApplicationContext) closeArgs[0]);
                        }
                    });
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            synchronized (GlobalVariable.initFinish) {
                ConfigurableApplicationContext run = SpringApplication.run(TransferFileApplication.class, args);
                closeArgs[0] = run;
                String property = SpringContextUtil.getProperty("dk.save-path");
                String userHome = System.getProperty("user.home");
                FileSystemResource resource = new FileSystemResource(Paths.get(userHome,property).toFile().getAbsolutePath());
                closeArgs[1] = resource;
                if (!resource.exists()) {
                    File file = resource.getFile();
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }
                log.info("config Path:" + resource.getFile().getAbsolutePath());
                PropertiesLoaderUtils.fillProperties(Constants.PROPERTIES, new EncodedResource(resource, "UTF-8"));
                GlobalVariable.initFinish.notifyAll();
                GlobalVariable.initFinish = true;
            }
            log.info("Start up complete");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            System.exit(0);
        }
    }
}
